<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Db;
// 应用公共文件
function api_go($code=0,$msg=null,$data=null)//输出api信息
{
    return json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data]);
}

function api_log($type,$text,$go=null,$to=null)//日志系统
{
    $info = [
        'people'=>$go,
        'topeople'=>$to,
        'type'=>$type,
        'text'=>$text,
        'time'=>time()
    ];
    return Db::name('log')->insert($info);
}
function get_client_ip($type = 0,$adv=true) {//获取用户IP
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if($adv){
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $pos    =   array_search('unknown',$arr);
    if(false !== $pos) unset($arr[$pos]);
    $ip     =   trim($arr[0]);
    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
    $ip     =   $_SERVER['HTTP_CLIENT_IP'];
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
    $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
    $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}

function get_ip_city($ip){//获取IP归属地
    $ch = curl_init();
    $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip='.$ip;
    //用curl发送接收数据
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //请求为https
    curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $location = curl_exec($ch);
    curl_close($ch);
    //转码
    $location = mb_convert_encoding($location, 'utf-8','GB2312');
    //var_dump($location);
    //截取{}中的字符串
    $location = substr($location, strlen('({')+strpos($location, '({'),(strlen($location) - strpos($location, '})'))*(-1));
    //将截取的字符串$location中的‘，’替换成‘&’   将字符串中的‘：‘替换成‘=’
    $location = str_replace('"',"",str_replace(":","=",str_replace(",","&",$location)));
    //php内置函数，将处理成类似于url参数的格式的字符串  转换成数组
    parse_str($location,$ip_location);
    return $ip_location['addr'];
}

function Gotoken($length){//生成一个Token
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
    $key = null;
	for($i=0;$i<$length;$i++){ 
		$key .= $pattern{mt_rand(0,35)}; //生成php随机数 
	} 
	return $key; 
}

function Sate_admin($user,$token)//通用管理身份验证
{
    $info = Db::name('admin')->where('username',$user)->find();
    if(!$info){return -1;}//账户异常
    if($info['sate']!=1){return -2;}//被封禁
    if($info['token']!=$token || $info['token_end'] < time()){
        return -3;//过期
    }
}

function Name_admin($user)//用户名取姓名
{
    $info = Db::name('admin')->where('username',$user)->find();
    if($info)
    {
        return $info['name'];
    }else{
        return $user;
    }
}